package org.example.demo.controller.userController;

import io.swagger.annotations.ApiOperation;
import org.example.demo.common.api.CommonResult;
import org.example.demo.dto.PullUserCartItems;
import org.example.demo.dto.ShopCartParam;
import org.example.demo.dto.ShopCartSubmitParam;
import org.example.demo.service.userService.ShopCartService;
import org.example.mbg.model.ProductByCart;
import org.example.mbg.model.ShopCart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping(value = "/user/cart", method = RequestMethod.GET)
@ResponseBody
public class ShopCartController {
    @Autowired
    private ShopCartService shopCartService;

    @ApiOperation("展示购物车")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PullUserCartItems>> showShopCarts(Long id) {
        System.out.println(id);
        return CommonResult.success(shopCartService.listShopCart(id));
    }

    @ApiOperation("修改商品数量")
    @RequestMapping(value = "/alter", method = RequestMethod.GET)
    @ResponseBody
    public String userListOrder(Long id, Long quantity) {
        return shopCartService.alterCart(id, quantity);
    }


    /*
    添加购物车
     */
    @ApiOperation("商品添加购物车")
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Integer> AddShopCart(Long user_id, Long product_id, Long count) {
        //成功返回1
        int operation_num = shopCartService.addShopCart(user_id, product_id, count);
        return CommonResult.success(operation_num);
    }

//    /*
//    删除购物车
//     */
//    @ApiOperation("删除购物车的某一项商品")
//    @RequestMapping(value = "/delete", method = RequestMethod.GET)
//    @ResponseBody
//    public CommonResult<Integer> DeleteShopCart(@RequestParam Long user_id, @RequestParam Long product_id) {
//        //成功返回1
//        int operation_num = shopCartService.deleteShopCart(user_id, product_id);
//        return CommonResult.success(operation_num);
//    }

    /*
    批量删除购物车
     */
    @ApiOperation("批量删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Integer> DeleteShopCart(@RequestBody List<Long> product_ids) {
        int operation_num = 0;
        for (Long product_id : product_ids) {
            operation_num += shopCartService.deleteShopCart(product_id);
        }
        return CommonResult.success(operation_num);
    }

    /*
    一键清空购物车（慎用）
     */
    @ApiOperation("清空购物车")
    @RequestMapping(value = "/clearall", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Integer> DeleteShopCart(@RequestParam Long user_id) {
        int operation_num = shopCartService.clearAll(user_id);
        return CommonResult.success(operation_num);
    }


//    //当用户点击结算的时候,直接返回一个初步订单对象，此时不存入订单表
//    @ApiOperation("当用户点击结算的时候,直接返回一个初步订单对象")
//    @RequestMapping(value = "/returnPreOrder", method = RequestMethod.POST)
//    @ResponseBody
//    public CommonResult<HashMap<String, Object>> shopCartToOrder(@Validated @RequestBody ShopCartParam shopCartParams) {
//        System.out.println(shopCartParams.getProductIds());
//        HashMap<String, Object> requestPreOrder = shopCartService.shopCartToOrder(shopCartParams);
//        List<ShopCartSubmitParam> scs = (List<ShopCartSubmitParam>) (requestPreOrder.get("ShopCarts"));
//        if (scs.isEmpty() || scs == null) {
//            return CommonResult.failed();
//        }
//        System.out.println("success");
//        return CommonResult.success(requestPreOrder);
//
//    }
}
